﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RsCms.Application;
using RsCms.Application.Posts.Dto;

namespace Plugin.RsCms.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PostsController : ControllerBase
    {
        IPostsAppService postAppService;
       
        public PostsController(IPostsAppService postAppService)
        {
            this.postAppService = postAppService;
           
        }
        /// <summary>
        /// 浏览帖子,最多20条
        /// </summary>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<object> GetList()
        {
            var model= await postAppService.PagePublishAsync(1, 20);
            return model.Items;
        }
        /// <summary>
        /// 己发布 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet("publish")]
        public async Task<object> PageAsync(int page,int pageSize,string date=null) 
        { 
            return await postAppService.PagePublishAsync(page, pageSize,date);
        }

        /// <summary>
        /// 阅读指定id的文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail")]
        public async Task<object> GetDetailAsync(string id)
        {
            return await postAppService.GetPostsAsync(id);
        }
        /// <summary>
        /// 获取帖子
        /// </summary>
        /// <param name="slug"></param>
        /// <returns></returns>
        [HttpGet("slug/{slug}")]
        public async Task<object> GetBySlug(string slug)
        {
            return await postAppService.GetPostsBySlugAsync(slug);
        }
        [HttpGet("tag")]
        public async Task<object> AllAsync(int page, int pageSize,string tag)
        {
            return await postAppService.PageAsync(page, pageSize,tag);

        }

        [Authorize("admin")]
        [HttpGet("drafts")]
        public async Task<object>DraftsAsync(int page,int pageSize, string date=null)
        {
            return await postAppService.PageDraftsAsync(page, pageSize, date);
        }
        [Authorize("admin")]
        [HttpGet("scheduled")]
        public async Task<object> ScheduledAsync(int page, int pageSize, string date=null)
        {
            return await postAppService.PageScheduledAsync(page, pageSize, date);
        }
        [Authorize("admin")]
        [HttpGet("all")]
        public async Task<object> AllAsync(int page, int pageSize)
        {
            return await postAppService.PageAsync(page, pageSize);

        }

        [Authorize("admin")]
        [HttpPost("edit")]
        public async Task<object>SavePostsAsync([FromBody]PostEditDto dto)
        {
            return await postAppService.SaveAsync(dto);
        }
        [Authorize("admin")]
        [HttpPost("delete")]
        public async Task<object> DeletePostsAsync(string id)
        {
            await postAppService.DeleteAsync(id);
            return "ok";  
        }

         
        [HttpGet("meta")]
        public async Task<object> GetPostsMetaAsync(string postId)
        {
            return await postAppService.GetPostMetaAsync(postId);
             
        }

        [Authorize("admin")]
        [HttpPost("meta")]
        public async Task<object> EditPostsMetaAsync(PostMetaDto dto)
        {
            return await postAppService.SavePostMetaAsync(dto);
             
        }
    }
}
